//
//  MovieViewController.m
//  TipCalculator
//
//  Created by Fox School of Business MIS on 9/19/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import <MediaPlayer/MEDIAPLAYER.H>
#import "MovieViewController.h"


@implementation MovieViewController

@synthesize doneButton;
@synthesize playVideoButton;

/* This code could go in loadview, however placing it there causes problems with using view transition animations */
- (id) init
{
	self.view = [[UIView alloc] initWithFrame: CGRectMake(0, 0, 320, 480)]; //view takes up whole screen
	self.view.backgroundColor = [UIColor blackColor]; 
	
	/* Add in a button that switches back to the tipcalculatorview when clicked */
	doneButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
	[doneButton setTitle: @"Tip Calculator" forState: UIControlStateNormal];
	doneButton.frame = CGRectMake(20, 300, 130, 30);
	[doneButton setTitleColor: [UIColor orangeColor] forState:UIControlStateNormal];
	[doneButton addTarget:self action:@selector(doneButtonTouched) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview: doneButton];
	

	/* Add in a button that plays the video again */
	playVideoButton = [UIButton buttonWithType: UIButtonTypeRoundedRect];
	[playVideoButton setTitle: @"Play Video" forState: UIControlStateNormal];
	playVideoButton.frame = CGRectMake(170, 300, 130, 30);
	[playVideoButton setTitleColor: [UIColor orangeColor] forState:UIControlStateNormal];
	[playVideoButton setTitleColor: [UIColor grayColor] forState:UIControlStateDisabled];
	[playVideoButton addTarget:self action:@selector(playMovie) forControlEvents:UIControlEventTouchUpInside];
	[self.view addSubview: playVideoButton];
	
	player = nil;
	return [super init];
}

-(void) setTipCalculatorView: (UIView*) view {

	tipCalculatorView = view;
}

/* Switches back to the tip calculator view */
-(void)doneButtonTouched {

	if(player != nil)
	{
		[player stop];	
		player = nil;
	}
	/* Create and run an animation for switching the view.  Set self as an animation delegate.  When the animation
	   is done running, switch the subview of the main window to the tipcalculator view */
	[UIView beginAnimations:@"change view to tipcalc" context:nil];
	[UIView setAnimationDelegate: self]; 
	[UIView setAnimationDuration: .55];
	[UIView setAnimationCurve: UIViewAnimationCurveLinear];
	[UIView setAnimationTransition: UIViewAnimationTransitionCurlUp forView: self.view.window cache:YES];

	UIWindow* window = self.view.window;
	[self.view removeFromSuperview];
	[window addSubview: tipCalculatorView];

	[UIView commitAnimations];
	
}


/* Called by button, and animationDidStop method (for transition animation to this view) of tipcalculatorviewcontroller */
- (void)playMovie {
	
	playVideoButton.enabled = NO;
	/* Create and inititialize movie player control */
	NSBundle *bundle = [NSBundle mainBundle];
	
	NSString *path = [bundle pathForResource:@"cat" ofType:@"mov"];
	NSURL *url = [NSURL fileURLWithPath:path];
	
	player = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
	player.scalingMode = MPMovieScalingModeAspectFill;
	player.movieControlMode = MPMovieControlModeHidden;
	/* Register for the playback finished notification; obtained from iphone application programming guide */
	[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(movieFinished:) name: MPMoviePlayerPlaybackDidFinishNotification object: player];

	[player play];
	/* When the player plays, it sets itself as the keyWindow.  Information obtained from: http://amromousa.com/2009/03/22/overlay-uiview-on-mpmovieplayercontroller/ */
	  
	UIWindow* window = [[UIApplication sharedApplication] keyWindow];
 	window.frame = CGRectMake(50, 35, 90, 90);
	window = nil;
}

/* Called when movie player is done playing; adapted from iphone application programming guide */
- (void)movieFinished: (NSNotification*) notification {

	[[NSNotificationCenter defaultCenter] removeObserver: self name: MPMoviePlayerPlaybackDidFinishNotification object: player];
	[player release];
	player = nil;
	
	playVideoButton.enabled = YES;
	
}

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
